﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Минуточка
{
    /// <summary>
    /// Interaction logic for wCalendar.xaml
    /// </summary>
    public partial class wCalendar : Window
    {
        public string Datemask {set; get;}

        public wCalendar()
        {
            InitializeComponent();

            
        }

        private void FillMaskAtOpen()
        {
            int i = Datemask.Length - Datemask.Replace("_", "").Length;
            switch (i)
            {
                case 12:
                    rdbMinute.IsChecked = true;
                    break;
                case 10:
                    rdbHour.IsChecked = true;
                    break;
                case 8:
                    rdbDay.IsChecked = true;
                    break;
                case 6:
                    rdbMonth.IsChecked = true;
                    break;
                case 4:
                    rdbYear.IsChecked = true;
                    break;
                default:
                    rdbNone.IsChecked = true;
                    break;
            }
            string now = DateTime.Now.ToString("dd.MM.yyyy HH:mm");
            string format = "";
            i = 0;
            foreach (char c in Datemask.ToCharArray())
            {
                if (c == '_')
                    format += now[i];
                else
                    format += c;
                i++;
            }
            DateTime d = DateTime.Parse(format);
            mcl.SelectionStart = d;
            dtpTime.Value = d;
            GenerateMask();
            
        }

        private void dtpTime_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //System.Windows.Forms.SendKeys.SendWait("Up");
        }

        private void mcl_DateChanged(object sender, System.Windows.Forms.DateRangeEventArgs e)
        {
            GenerateMask();
        }

        private void dtpTime_ValueChanged(object sender, EventArgs e)
        {
            GenerateMask();
        }

        void GenerateMask()
        {
            DateTime d = new DateTime(mcl.SelectionStart.Year,
                                        mcl.SelectionStart.Month,
                                        mcl.SelectionStart.Day,
                                        dtpTime.Value.Hour,
                                        dtpTime.Value.Minute,
                                        00);
            string format="";
            
            if ((bool)rdbNone.IsChecked)
            {
                format = "dd.MM.yyyy HH:mm";
            }
            else if ((bool)rdbYear.IsChecked)
            {
                format = "dd.MM.____ HH:mm";
            }
            else if ((bool)rdbMonth.IsChecked)
            {
                format = "dd.__.____ HH:mm";
            }
            else if ((bool)rdbDay.IsChecked)
            {
                format = "__.__.____ HH:mm";
            }
            else if ((bool)rdbHour.IsChecked)
            {
                format = "__.__.____ __:mm";
            }
            else if ((bool)rdbMinute.IsChecked)
            {
                format = "__.__.____ __:__:59";
            }
            

            Datemask = d.ToString(format);
            if (this.IsInitialized) txtOut.Text = Datemask;
        }

        private void rdbPeriod_Checked(object sender, RoutedEventArgs e)
        {
            GenerateMask();
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            GenerateMask();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FillMaskAtOpen();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

    }
}
